package com.company.project.common.job.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.utils.Constant;
import com.company.project.entity.EJkzGrdjxxEntity;
import com.company.project.entity.EJkzTjxxEntity;
import com.company.project.service.EJkzGrdjxxService;
import com.company.project.service.EJkzTjxxService;
import com.company.project.service.MedicalInformationTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 *  体检信息上传
 * @author liuhui
 * @Created 2020-11-13
 */
@Component("medicalInformationTask")
@Slf4j
public class MedicalInformationTask {
    @Resource
    private EJkzTjxxService eJkzTjxxService;
    @Resource
    private MedicalInformationTaskService medicalInformationTaskService;
    @Resource
    private EJkzGrdjxxService eJkzGrdjxxService;

    public void run(String params){
        log.info("健康证信息-体检信息上传,参数：{}", params);
        int successNum =0,failNum = 0;
        LambdaQueryWrapper<EJkzTjxxEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EJkzTjxxEntity::getUploadStatus, 0);
        List<EJkzTjxxEntity> list = eJkzTjxxService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (EJkzTjxxEntity item : list) {
            LambdaQueryWrapper<EJkzGrdjxxEntity> eJkzGrdjxxEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
            eJkzGrdjxxEntityLambdaQueryWrapper.eq(EJkzGrdjxxEntity::getUploadStatus, Constant.DATA_UPLOAD)
                    .eq(EJkzGrdjxxEntity::getWbId,item.getPractitionersBaseId());
            EJkzGrdjxxEntity grdjxxEntity = eJkzGrdjxxService.getOne(eJkzGrdjxxEntityLambdaQueryWrapper);
            if (grdjxxEntity == null) {
                continue;
            }
            boolean uploadResult = medicalInformationTaskService.uploadMedicalInformation(item,grdjxxEntity);
            if (uploadResult) {
                successNum++;
                continue;
            }
            failNum++;
        }
        log.info("健康证信息-体检信息上传,待上传数：{} ,成功上传{}", failNum+successNum, successNum);
    }
}
